package com.woniu.houtai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.houtai.entity.Business;
import com.woniu.houtai.mapper.BusinessMapper;
import com.woniu.houtai.service.BusinessService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    BusinessMapper businessMapper;
    @Override
    public Integer register(String name, String password, String email, String imgSrc, String tel) {
        System.out.println("service中注册");
        Business business = new Business();
        business.setName(name);
        business.setPassword(password);
        business.setEmail(email);
        business.setImgSrc(imgSrc);
        business.setTel(tel);
        return businessMapper.insert(business);
    }

    // 根据用户名和密码登录
    @Override
    public Integer loginByName(String name, String password) {

        // 根据用户名查询用户
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        // 0代表用户不存在，1代表密码错误，2.代表登录成功
        Business business = businessMapper.selectOne(queryWrapper);
        if(business == null){
            return 0;
        }else if(!business.getPassword().equals(password)){
            return 1;
        }else {
            return 2;
        }

    }

    @Override
    public String confirmExist(String username, String password, String email, String tel) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        Business business = businessMapper.selectOne(queryWrapper);
        String result = null;
        if (business != null) {
            result = "用户名已被占用";
        }
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        business = businessMapper.selectOne(queryWrapper);
        if (business != null) {
            result = "邮箱已使用";
        }
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", tel);
        business = businessMapper.selectOne(queryWrapper);
        if (business != null) {
            result = "电话被占用";
        }
        return result;
    }

    @Override
    public String confirmUsernameExist(String username) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        Business business = businessMapper.selectOne(queryWrapper);
        String result = null;
        if (business != null) {
            result = "用户名已被占用";
        }
        return result;
    }


    // 根据用户名查询用户
    @Override
    public Business getBusinessByName(String name) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return businessMapper.selectOne(queryWrapper);
    }

    @Override
    public Business getBusinessByEmail(String email) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        return businessMapper.selectOne(queryWrapper);
    }

    @Override
    public Business getBusinessByTel(String tel) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel",tel);
        return businessMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updatePassword(Integer id, String password) {
        Business business = businessMapper.selectById(id);
        business.setPassword(password);

        return businessMapper.updateById(business);
    }

    @Override
    public Integer update(Integer id,String name, String password, String email, String img_src, String tel) {
        QueryWrapper<Business> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Business business = businessMapper.selectOne(queryWrapper);
        business.setName(name);
        business.setPassword(password);
        business.setEmail(email);
        business.setImgSrc(img_src);
        business.setTel(tel);
        return businessMapper.updateById(business);
    }





}
